<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-item">
        <span>机构名称:</span>
        <el-input v-model="listQuery.name" clearable placeholder="请输入机构名称" style="width: 200px" />
      </div>
      <div class="filter-item">
        <span>地理编码:</span>
        <el-input v-model="listQuery.geocoding" clearable placeholder="请输入地理编码" style="width: 200px" />
      </div>
      <el-button class="filter-item" type="primary" icon="Search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>
    </div>
    <div class="operationList">
      <div>
        <el-button v-permission="['test:treeTable:TreeTable:add']" text type="primary" icon="Plus" @click="handleCreate">新增</el-button>
      </div>
      <div>
        <el-button text type="primary" icon="RefreshRight" @click="handleFilter">刷新</el-button>
      </div>
    </div>

    <el-table v-loading="listLoading" :row-class-name="tableRowClass" :data="list" row-key="id" highlight-current-row header-cell-class-name="header-cell">
      <el-table-column label="机构名称" min-width="150px">
        <template #default="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="父节点" min-width="150px">
        <template #default="{row}">
          <span>{{ row.parentId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地理编码" min-width="150px">
        <template #default="{row}">
          <span>{{ row.geocoding }}</span>
        </template>
      </el-table-column>
      <el-table-column label="邮政编码" min-width="150px">
        <template #default="{row}">
          <span>{{ row.postalCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="230" class-name="small-padding fixed-width">
        <template #default="{row}">
          <el-button v-permission="['test:treeTable:TreeTable:update']" size="small" plain type="primary" icon="EditPen" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-permission="['test:treeTable:TreeTable:delete']" size="small" plain type="danger" icon="Delete" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <tree-table-form ref="form" @refresh-list="refreshList" />

  </div>
</template>

<script>
import { deleteTreeTable, fetchTreeTableList } from '@/api/demo/treeTable/treeTable'
import treeTableForm from './treeTableForm'
import { treeMixin } from '@/mixin'
export default {
  name: 'TreeTable',
  components: { treeTableForm },
  mixins: [treeMixin],
  data() {
    return {
      list: [],
      listLoading: true,
      listQuery: {
        name: undefined,
        geocoding: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 获取列表
     */
    getList(loading = true) {
      this.listLoading = loading
      this.list = []
      fetchTreeTableList(this.listQuery).then(response => {
        if (response.data.success) {
          this.list = response.data.data
        } else {
          this.$message.error(response.data.msg)
        }
        this.listLoading = false
      })
    },
    /**
     * 获取列表
     */
    refreshList(row) {
      const result = {}
      this.searchTree(this.list, row, result)
      this.refreshTree(this.list, row, result)
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 新增
     */
    handleCreate() {
      this.$refs.form.handleCreate()
      this.$refs.form.setList(this.list)
    },
    /**
     * 更新
     */
    handleUpdate(row) {
      this.$refs.form.setList(this.list)
      this.$refs.form.handleUpdate(row.id)
    },
    /**
     * 删除
     */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTreeTable([row.id]).then(response => {
          if (response.data.success) {
            this.$message.success(response.data.msg)
            this.deleteTree(this.list, row)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    }
  }
}
</script>
